using AbpTmp.EntityFrameworkCore;
using AbpTmp.MultiTenancy;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.IO;
using Volo.Abp;
using Volo.Abp.AspNetCore.MultiTenancy;
using Volo.Abp.AspNetCore.Serilog;
using Volo.Abp.Autofac;
using Volo.Abp.AutoMapper;
using Volo.Abp.Modularity;
using Volo.Abp.UI.Navigation.Urls;
using Volo.Abp.VirtualFileSystem;

namespace AbpTmp.Web;

[DependsOn(
    typeof(AbpTmpApplicationContractsModule),  
    typeof(AbpTmpApplicationModule),
    typeof(AbpTmpEntityFrameworkCoreModule),
    typeof(AbpAutofacModule),     
    typeof(AbpAspNetCoreSerilogModule)  ,
	typeof(AbpAspNetCoreMultiTenancyModule)
	)]
public class AbpTmpWebModule : AbpModule
{
   

    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        var hostingEnvironment = context.Services.GetHostingEnvironment();
        var configuration = context.Services.GetConfiguration();

        //ConfigureAuthentication(context);
        ConfigureUrls(configuration); 
        ConfigureAutoMapper();
        ConfigureVirtualFileSystem(hostingEnvironment);
        
    }

     

    private void ConfigureUrls(IConfiguration configuration)
    {
        Configure<AppUrlOptions>(options =>
        {
            options.Applications["MVC"].RootUrl = configuration["App:SelfUrl"];
        });
    }

    

    private void ConfigureAutoMapper()
    {
        Configure<AbpAutoMapperOptions>(options =>
        {
            options.AddMaps<AbpTmpWebModule>();
        });
    }

    private void ConfigureVirtualFileSystem(IWebHostEnvironment hostingEnvironment)
    {
        if (hostingEnvironment.IsDevelopment())
        {
            Configure<AbpVirtualFileSystemOptions>(options =>
            {
                options.FileSets.ReplaceEmbeddedByPhysical<AbpTmpDomainSharedModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}AbpTmp.Domain.Shared"));
                options.FileSets.ReplaceEmbeddedByPhysical<AbpTmpDomainModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}AbpTmp.Domain"));
                options.FileSets.ReplaceEmbeddedByPhysical<AbpTmpApplicationContractsModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}AbpTmp.Application.Contracts"));
                options.FileSets.ReplaceEmbeddedByPhysical<AbpTmpApplicationModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}AbpTmp.Application"));
                options.FileSets.ReplaceEmbeddedByPhysical<AbpTmpWebModule>(hostingEnvironment.ContentRootPath);
            });
        }
    }

     

    //private void ConfigureAutoApiControllers()
    //{
    //    Configure<AbpAspNetCoreMvcOptions>(options =>
    //    {
    //        options.ConventionalControllers.Create(typeof(AbpTmpApplicationModule).Assembly);
    //    });
    //}

    

    public override void OnApplicationInitialization(ApplicationInitializationContext context)
    {
        var app = context.GetApplicationBuilder();
        var env = context.GetEnvironment();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseAbpRequestLocalization();

        if (!env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

       
        app.UseStaticFiles();
        app.UseRouting();
        app.UseAuthentication();
        

        if (MultiTenancyConsts.IsEnabled)
        {
            app.UseMultiTenancy();
        }

        app.UseUnitOfWork();
        
        app.UseAuthorization();       

        app.UseAuditing();
        app.UseAbpSerilogEnrichers();
        app.UseConfiguredEndpoints();
    }
}
